﻿using Core.DataAccess.Model;
using Dapper;
using Grain.Interfaces;
using Grain.State.StateEntitys;
using iTool.ClusterComponent;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Grain.Implements
{
    public class EditCloudFunctionListService : iToolServiceStorageBase<List<EditCloudFunctionListState>>, IEditCloudFunctionListService
    {
        private IDBFactory iDBFactory;

        public EditCloudFunctionListService(IDBFactory iDBFactory)
        {
            this.iDBFactory = iDBFactory;
        }


        public Task AddTemporaryFunction(bool isUseState, string modulePath, string channel)
        {
            // 新增文件切换则保存
            // 所有！！！切换则丢失编辑状态
            this.State.Add(new EditCloudFunctionListState
            {
                isUseState = isUseState,
                modulePath = modulePath
            });

            return Task.CompletedTask;
        }

        public Task EnterEditState(string modulePath, string channel)
        {
            foreach (var item in this.State)
            {
                if (item.modulePath == modulePath)
                {
                    item.IsEditState = true;
                    item.EditChannel = channel;
                    return Task.CompletedTask;
                }
            }

            return Task.CompletedTask;
        }

        public Task ExitEditState(string modulePath)
        {
            foreach (var item in this.State)
            {
                if (item.modulePath == modulePath)
                {
                    item.IsEditState = false;
                    item.EditChannel = string.Empty;
                    return Task.CompletedTask;
                }
            }

            return Task.CompletedTask;
        }

        public Task<List<EditCloudFunctionListState>> GetFilesDetail()
        {
            return Task.FromResult(this.State);
        }

        public Task RemoveFunction(string modulePath)
        {
            this.State = this.State.Where(item => item.modulePath != modulePath).ToList();

            using (var db = this.iDBFactory.Create(DatabaseType.Function))
            {
                try
                {
                    db.Execute("delete [Function] where moduleName = @moduleName", new { moduleName = modulePath });
                }
                catch (Exception ex)
                {
                    _ = ex;
                }
            }

            return Task.CompletedTask;
        }

        protected override Task ClearStateAsync()
        {
            return Task.CompletedTask;
        }

        protected override Task ReadStateAsync()
        {
            using (var db = this.iDBFactory.Create(DatabaseType.Function))
            {
                this.State = db.Query<EditCloudFunctionListState>($"select moduleName as modulePath,isUseState from [Function]").AsList();
            }

            return Task.CompletedTask;
        }

        protected override Task WriteStateAsync()
        {
            return Task.CompletedTask;
        }
    }
}
